package unamur.iodass.data;

import java.io.BufferedReader;
import java.nio.charset.Charset;
import java.util.ArrayList;

import unamur.iodass.app.ProjectProperties;
import unamur.iodass.catalog.CSVBookParser;
import unamur.iodass.communication.messages.Book;


/**
 * Cette class permet de créer un catalog
 * 
 * @author Cherchem, Marchal
 *
 */


public class CatalogData {
	
	private ArrayList<Book> books;
	
	
	public CatalogData() {
		books = getAllBooks();
	}
	

   private ArrayList<Book> getAllBooks() {
    	CSVBookParser parser = new CSVBookParser();
    	
    	
    	BufferedReader buffer = null;
    	buffer = parser.open(ProjectProperties.getProjectProperty("fichierCSV"), Charset.forName("UTF-8"));
    	ArrayList<Book> books = parser.parse(buffer);
    	
    	return books;
    }
    
    public Book getBookByISBN(long isbn) {
    	for (int i=0; i<books.size(); i++) {
    		if (books.get(i).getIsbn() == isbn) {
    			return books.get(i);
    		}
    	}
    	
    	return null;
    }
    
    
    public ArrayList<Book> getBooks() {
    	return this.books;
    }

}
